/**
 * 设置
 */
var publicUtils = require("script/localStorageManagers.js");
var localStoragerManager = require('script/localStorageManagers.js');
var mobileFrame = require('scripts/Core/MobileFrame.js');
mui.init({
	swipeBack: true
});
//加载plusReady
if (window.plus) {
	plusReady();
} else {
	document.addEventListener("plusready", plusReady, false);
}
//plusReady
function plusReady() {
	//懒加载
	//mui(document).imageLazyload({
	//	placeholder: '../images/user_guest.png'
	//});
	initSetupinfo();
	//头像
	mui('.list').on('tap', '.exchangetouxiang', exchangeTouxiang);
	//设置缓存大小
	setCache();
	//修改姓名
	mui('.list').on('tap', '.editusername', function() {
		var username = document.getElementById('username').innerHTML;
		mui.openWindow({
			url: 'editusername.html',
			id: 'setup/editusername',
			styles:getStyles(),
			extras: {
				username: username
			}
		});
	});
	//清除缓存
	mui('.list').on('tap', '#clearCache', function() {
		 mui.confirm('确定清除缓存吗？', '', null, function(e) {
		     if (e.index == 0) {
			     document.getElementById('cachevalue').innerHTML = '0M';
		publicUtils.clearAllCache(
			function(successInfo) {
				mui.toast(successInfo);
			},
			function(errorInfo) {
				mui.toast(errorInfo);
			});
		       } 
	      });
		

	});
	//帐号与安全
	mui('.list').on('tap','.setaccount',function(){
		mui.openWindow({
			url:'accountindex.html',
			id:'setup/accountindex',
			styles:getStyles()
		});
	});
	//意见反馈
	mui('.list').on('tap', '#feedback', function() {
		mui.openWindow({
			url: 'feedback.html',
			styles:getStyles(),
			id: 'setup/feedback'
		});
	});
	//评价
	mui('.list').on('tap', '#pingjia', function() {
		if (mui.os.ios) {
			location.href = 'https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews';
		} else if (mui.os.android) {
			plus.runtime.openURL("market://details", function(e) {
				plus.runtime.openURL("market://details", function(e) {
					mui.toast("当前应用未上传到应用商店，暂时无法评分，感谢支持");
				}, "com.qihoo.appstore");
			}, "com.tencent.android.qqdownloader");
		};

	});
	//关于
	mui('.list').on('tap', '#about', function() {
		mui.openWindow({
			url: 'about.html',
			styles:getStyles(),
			id: 'setup/about'
		});
	});
	//退出登录
	mui('.list').on('tap', '#exitmooc', function() {
		 mui.confirm('确定退出登录吗？', '', null, function(e) {
		     if (e.index == 0) {
			     publicUtils.clearUserinfo();
		         mui.openWindow({
			       url: '../login.html',
			       styles:getStyles(),
			       id: 'login'
		        });
		       } 
	      });
	});
};
function initSetupinfo(){
	//设置头像
	setUsertouxiang();
	//设置用户基本信息
	setUserinfo();
	//性别下拉框
	setSexPicker();
	//设置缓存大小
	setDownsize();
}
//设置用户基本信息‘
function setUserinfo() {
	var userinfo = localStoragerManager.getUserinfoSession();
	//用户姓名
	setUsername(userinfo.username);
	//性别
	var sex = userinfo.sex;
	if (sex == 0) {
		sex = '男';
	} else {
		sex = '女';
	};
	document.getElementById('usersex').innerHTML = sex;
	//头像
};
//设置用户名
function setUsername(username) {
	document.getElementById('username').innerHTML = username;
}
//设置头像
function setUsertouxiang() {
	var localStorageManagers = require("script/localStorageManagers.js");
	var userinfo = localStorageManagers.getUserinfoSession();
	var userpath =userinfo.path;
	console.log("1:"+userpath);
	//document.getElementById('userimg').setAttribute('data-lazyload', userpath);
	if(userpath!=null&&userpath.length>0)document.getElementById('userimg').setAttribute('src', fileServerAjax+userpath);
};
//设置头像
function setTouxiang(path){
	document.getElementById('userimg').setAttribute('src', path);
}
//切换头像
function exchangeTouxiang() {
	if(!mobileFrame.IsNetWorkCanUse())return;
	var btnArray = [{
		title: "拍照"
	}, {
		title: "从相册选择"
	}];
	plus.nativeUI.actionSheet({
		//title:"选择照片",
		cancel: "取消",
		buttons: btnArray
	}, function(e) {
		switch (e.index) {
			case 0:
				break;
			case 1:
				getImage();
				break;
			case 2:
				galleryImg();
				break;
			default:
				break
		}
		//info.innerHTML = text+"\"按钮";
	});
};
//性别下拉框
function setSexPicker() {
	var userPicker = new mui.PopPicker();
	userPicker.setData([{
		value: '0',
		text: '男'
	}, {
		value: '1',
		text: '女'
	}]);
	mui('.mui-content').on('tap', '.sexpicker', function() {
		userPicker.show(function(items) {
			if(!mobileFrame.IsNetWorkCanUse())return;
			updateSex(items);
		});
	});
};
//设置缓存大小
function setDownsize(){
	var localStorageManagers = require('script/localStorageManagers.js');
}
//修改性别
function updateSex(items) {
	var sex = items[0].value;
	var sextext = items[0].text;
	var userid = localStoragerManager.getUseridSession();
	var url = serverAjax + "/mobile/user/updateUserinfo.do";
	mui.ajax(url, {
		data: {
			sex: sex,
			userid: userid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data != null) {
				localStoragerManager.setUserinfoSession(data.ext1);
				document.getElementById('usersex').innerHTML = sextext;
			}
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};

//打开相机--参考setting.html
function getImage() {
	var c = plus.camera.getCamera();
	c.captureImage(function(e) {
		plus.io.resolveLocalFileSystemURL(e, function(entry) {
			var s = entry.toLocalURL();
			printscreenHeadImage(s);
		}, function(e) {
			console.log("读取拍照文件错误：" + e.message);
		});
	}, function(s) {
		console.log("error" + s);
	}, {
		filename: "_doc/camera/"
	})
}
//打开相册
function galleryImg() {
				// 从相册中选择图片
					plus.gallery.pick(function(path) {
						if (0 != path.indexOf("file://")) {
							path = "file://" + path;
						}
							printscreenHeadImage(path);
					}, function(e) {}, {
						filter: "image"
					});
		};
		
function printscreenHeadImage(path){
				mui.openWindow({
					url: 'change_photo.html',
					id: 'change_photoid',
					styles:getStyles(),
					show: {
						autoShow: true,
						aniShow: "slide-in-bottom"
					},
					extras: {
						headImageName: path
					}
				})
			}
//设置缓存大小
function setCache() {
	plus.io.resolveLocalFileSystemURL("_downloads/downloadFiles2/", function(entry) {
		entry.getMetadata(function(metadata) {
			var s = metadata.size / (1024 * 1024);
			document.getElementById('cachevalue').innerHTML = s.toFixed(1) + "M";
		}, function(err) {
			console.log("获取文件大小失败");
		})
	}, function(err) {
		console.log(err.message);
	})
}
